package com.op.teachingliteapp.service;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences.Editor;

import com.android.volley.Response;
import com.op.teachingliteapp.bean.UserBean;
import com.op.teachingliteapp.common.Common;
import com.op.teachingliteapp.common.api.teachinglite.UserUrl;
import com.op.teachingliteapp.database.dao.UserDao;
import com.op.teachingliteapp.util.SharedPreferencesUtils;
import com.op.teachingliteapp.util.http.VolleySingleton;

public class LoginService extends BaseService {

	public LoginService(Context context, String response) {
		this.context = context;
		parse(response);
	}

	@Override
	protected void parse(String response) {
		try {
			JSONObject jsonObject = new JSONObject(response);
			if (!jsonObject.isNull(Common.JsonResult.RESULT_MAP)) {
				content = jsonObject.getJSONObject(Common.JsonResult.RESULT_MAP).getJSONObject(
						Common.JsonResult.LOGIN_USER);
			}
			message = jsonObject.getString(Common.JsonResult.MESSAGE);
			status = jsonObject.getString(Common.JsonResult.STATUS);
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 是否自动登录
	 */
	public static boolean isAutoLogin() {
		return SharedPreferencesUtils.getPreference().getBoolean(Common.Prefs.IS_AUTO_LOGIN, false);
	}

	/**
	 * 是否保存密码
	 */
	public static boolean isSavePwd() {
		return SharedPreferencesUtils.getPreference().getBoolean(Common.Prefs.IS_SAVE_PASSWORD,
				false);
	}

	/**
	 * 发送用户编号、密码，联网登录
	 */
	public static void syncLogin(String userNumber, String pwd, Response.Listener<String> listener,
			Response.ErrorListener errorListener) {
		Map<String, String> params = new HashMap<String, String>();
		params.put(Common.Post.USER_NUMBER, userNumber);
		params.put(Common.Post.USER_PWD, pwd);
		VolleySingleton.getInstance().load(UserUrl.LOGIN, params, listener, errorListener);
	}

	/**
	 * 注销
	 */
	public static void logout() {
		Editor edit = SharedPreferencesUtils.getEditor();
		edit.remove(Common.Prefs.USER_NUMBER);
		edit.remove(Common.Prefs.USER_PWD);
		edit.remove(Common.Prefs.USER_ID);
		edit.remove(Common.Prefs.USER_NAME);
		edit.remove(Common.Prefs.USER_IMAGE_URL);
		edit.commit();
	}

	/**
	 * 是否同步服务器登录成功
	 */
	public boolean isSyncLogin() {
		return getMessage().equals(Common.JsonResult.LOGIN_SUCCESS);
	}
	
	/**
	 * 保存登录信息
	 */
	public void saveData() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				// 1. 将JsonObject存入本地数据库
				UserBean userBean = new UserBean();
				userBean.fillAttributeByJson(getContent());
				UserDao userDao = new UserDao(context);
				userDao.insertOrUpdate(userBean);

				// 2. 将当前登录的用户信息保存到SharedPreference
				Editor editor = SharedPreferencesUtils.getEditor();
				editor.putString(Common.Prefs.USER_ID, userBean.getId());
				editor.putString(Common.Prefs.USER_NAME, userBean.getName());
				editor.putString(Common.Prefs.USER_NUMBER, userBean.getNumber());
				editor.putString(Common.Prefs.USER_PWD, userBean.getPassword());
				editor.putString(Common.Prefs.USER_IMAGE_URL, userBean.getImageUrl());
				editor.commit();
			}
		}).start();
	}

}
